// pages/minePage/checkNumber/checkNumber.js
import throttle from '../../../utils/throttle.js';
import Http from '../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        uid: '',
        pageNum: 0,
        chackArr: [],
        sceneImageArr: [],
        description: [],
        currentPicIndex: 0,
        loading: false,
        loadingAll: false,
        checkinyear: 0,
        dataIndex: 0,
        lastest: {
            review_title: '暂无消息',
        }, // 最近第一条审核消息
        unreadCount: 0, // 未读消息数
        showNewBox: true, //产看他人主页隐藏消息提示
        // 更多操作
        show: false,
        actions: [
            {
                id: '01',
                name: '删除',
                color: '#ff455e',
            },
        ],
        activeRecordId: '', //打卡记录id
        isMineRecord: false, //是自己的打卡记录
    },
    previewImgTap(e) {
        this.setData({
            sceneImageArr: e.currentTarget.dataset.srcarr,
            description: e.currentTarget.dataset.descarr,
        });
        //hideBottom隐藏图片预览的删除按钮
        wx: wx.navigateTo({
            url: `../../checkIn/imgPreview/imgPreview?hideBottom=${true}`,
        });
    },
    jumpInfotap(e) {
        wx.navigateTo({
            url: `../../../functionPage/pages/minePage/checkInIfo/checkInIfo?id=${e.currentTarget.dataset.id}&uid=${this.data.uid}`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.checkinname) {
            wx.setNavigationBarTitle({
                title:
                    '打卡数·' +
                    options.checkinnum +
                    '（' +
                    options.checkinname +
                    '）',
            });
        } else {
            wx.setNavigationBarTitle({
                title: '打卡数·' + options.checkinnum,
            });
        }
        this.setData({
            uid: options.uid,
            showNewBox: !options.uid,
        });
        this.getCheckInfo();
    },

    onShow: function () {
        this.getLatestVerifyMsg();
    },

    getCheckInfo(e) {
        let _that = this;
        if (!_that.data.loadingAll) {
            _that.setData({
                pageNum: _that.data.pageNum + 1,
                loading: true,
                loadingAll: false,
            });
            wx.request({
                url: app.globalData.rootPath + `cjMap/getCheckinList`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    uid: _that.data.uid || '',
                    checkinuid: '00',
                    checkintimeorder: 'desc',
                    pageSize: 10,
                    pageNo: _that.data.pageNum,
                },
                success: function (res) {
                    if (res.data.status == 0) {
                        if (_that.data.isMineRecord) {
                            wx.setNavigationBarTitle({
                                title: '打卡数·' + res.data.pager.totalRows,
                            });
                        }
                        // 去除入职时间   添加数据
                        _that.data.chackArr.pop();
                        //获取打卡数列表
                        let checkinyears;
                        let checkintimeDay;
                        for (let i = 0; i < res.data.data.length; i++) {
                            checkinyears = res.data.data[i].checkintime.slice(
                                0,
                                4
                            );
                            checkintimeDay = res.data.data[i].checkintime.slice(
                                8,
                                10
                            )
                                ? res.data.data[i].checkintime.slice(8, 10) +
                                  '日'
                                : '';
                            res.data.data[i].checkintime =
                                res.data.data[i].checkintime.slice(5, 7) +
                                '月' +
                                checkintimeDay;
                            if (checkinyears == _that.data.checkinyear) {
                                _that.data.chackArr.push(res.data.data[i]);
                            } else {
                                console.log(
                                    res.data.chinininfo[_that.data.dataIndex]
                                );
                                _that.data.chackArr.push(
                                    res.data.chinininfo[_that.data.dataIndex]
                                );
                                _that.data.chackArr.push(res.data.data[i]);
                                _that.setData({
                                    checkinyear: checkinyears,
                                    dataIndex: _that.data.dataIndex + 1,
                                });
                            }
                        }
                        //获取入职时间
                        let entryData = {
                            entryData: '',
                        };
                        let entryYear =
                            res.data.entrydate.slice(0, 4) || 'XXXX';
                        let entryMon = res.data.entrydate.slice(5, 7) || 'XX';
                        let entryDay = res.data.entrydate.slice(8, 10) || 'XX';
                        entryData.entryData =
                            entryYear +
                            '年' +
                            entryMon +
                            '月' +
                            entryDay +
                            '日加入湛腾测试团队';
                        _that.data.chackArr.push(entryData);
                        _that.setData({
                            chackArr: _that.data.chackArr,
                            loading: false,
                        });
                        console.log(_that.data.chackArr);
                        if (res.data.data.length < 10) {
                            _that.setData({
                                loadingAll: true,
                            });
                        }
                    } else {
                        if (res.data.errmsg == '输入页码超出范围') {
                            _that.setData({
                                loadingAll: true,
                                loading: false,
                            });
                        }
                    }
                },
            });
        }
    },

    changeNav: throttle(
        (e) => {
            let { nav } = e.currentTarget.dataset;
            console.log(nav);
            wx.navigateTo({
                url: `${nav}`,
            });
        },
        200,
        {
            trailing: false,
        }
    ),

    // 获取最新审核消息
    getLatestVerifyMsg() {
        let data = {
            authId: app.globalData.authId,
            origintype: '04',
        };
        http.getRequest('cjMap/getLatestVerifyMsg', data).then((res) => {
            if (res.status == 0) {
                let lastest = res.data;
                if (lastest && lastest.type) {
                    // 处理审核消息标题
                    if (lastest.type === '08') {
                        lastest.review_title = '您的补充打卡通过了审核';
                    }
                    if (lastest.type === '09') {
                        lastest.review_title = '抱歉，您的补充打卡未通过审核';
                    }
                    if (lastest.type === '10') {
                        lastest.review_title = '恭喜，您的补充打卡被设置为优秀';
                    }
                    this.setData({
                        lastest,
                    });
                }
                this.setData({
                    unreadCount: res.count,
                });
            }
        });
    },

    edit(e) {
        let { id, index, createuid } = e.currentTarget.dataset;
        if (id && createuid === app.globalData.uid) {
            // 首尾是没有id的
            this.setData({
                show: true,
                activeRecordId: id,
                isMineRecord: true,
            });
        }
    },

    onCancel() {
        this.setData({
            show: false,
        });
    },

    onSelect(e) {
        let _that = this;
        if (e.detail.id === '01') {
            _that.onCancel();
            wx.showModal({
                title: '确认删除？',
                content: '',
                success: function (res) {
                    if (res.confirm) {
                        _that.deleteCheckInRecord(_that.data.activeRecordId);
                    } else {
                        console.log('取消操作');
                    }
                },
            });
        }
    },

    deleteCheckInRecord(id) {
        let _that = this;
        http.getRequest(
            `cjMap/deleteCheckinRecord?authId=${app.globalData.authId}&id=${id}&type=01`
        ).then((res) => {
            if (res.status === '0') {
                // 取消弹框
                _that.onCancel();
                // 删除成功后就刷新
                _that.refresh();
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 1500,
                });
            } else {
                console.log('删除失败');
            }
        });
    },

    refresh() {
        this.setData(
            {
                pageNum: 0,
                loading: false,
                loadingAll: false,
                chackArr: [],
                checkinyear: 0,
                dataIndex: 0,
            },
            () => {
                this.getCheckInfo();
                this.getLatestVerifyMsg();
            }
        );
    },
});
